<?php
use yii\helpers\Html;
use yii\web\View;
use yii\widgets\ActiveForm;
use yii\widgets\Breadcrumbs;
use app\modules\core\widgets\SlideNav;

$fieldIds = $model->fieldAttrs ? json_encode($model->fieldAttrs) : json_encode([]);

$this->registerJs(
"
function fl(){}
fl.allFields = {};
fl.catchFields = {$fieldIds};
fl.addField = function(anchor, id){
    if (! (id in fl.allFields)) {
        return false;
    }

    anchor.text = '已添加';
    anchor.onclick = null;

    if ($.inArray(id, fl.catchFields) >= 0) {
        return false;
    }

    fl.catchFields.push(id);
    fl.insertNode(fl.allFields[id]);
};
fl.insertNode = function(field) {
    var node = [];

    node.push('<li class=\"list-group-item\">');
    node.push('<input type=\"hidden\" name=\"fields[]\" value=\"' + field['_id'] + '\" />');
    node.push('<span>' + field['name'] + '</span>');
    node.push('<span>' + field['desc'] + '</span>');
    node.push('<span class=\"glyphicon glyphicon-remove pull-right\" onclick=\"fl.delField(this, \'' + field['id'] + '\')\"></span>');
    node.push('</li>');

    $('#fieldList').append(node.join(''));
}
fl.delField = function(node, id) {
    $(node.parentNode).remove();

    var pos = $.inArray(id, fl.catchFields);
    if (pos >= 0) {
        fl.catchFields.splice(pos, 1);
    }
}

$('#myModal').on('show.bs.modal', function (){
        $('#myModal .modal-body table tr:gt(0)').remove();
        
        $.getJSON('/core/field/all', function (data){
            $.each(data, function (i, item){
                var tds = [];

                fl.allFields[item['id']] = item;

                tds.push('<tr>');
                tds.push('<td>' + item['id'] + '</td>');
                tds.push('<td>' + item['name'] + '</td>');
                tds.push('<td>' + item['desc'] + '</td>');
                tds.push('<td>' + item['type'] + '</td>');
                tds.push('<td><a class=\"btn\" href=\"javascript:void(0);\" onclick=\"fl.addField(this, \'' + item['id'] + '\')\">添加</a></td>');
                tds.push('</tr>');
                $('#myModal .modal-body table').append(tds.join(''));
            })
        });
});
",
View::POS_END
);
?>
<div class="container-fluid">
	<div class="row">
        <?= SlideNav::widget() ?>

		<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2 main">
            <?= Breadcrumbs::widget([
                'homeLink' => ['label' => 'HOME', 'url' => '/core'],
                'links' => [
                    ['label' => '模型', 'url' => '/core/model/list'],
                    '编辑',
                ]
            ]) ?>
			
			<div class="row">
				<div class="col-md-12 col-lg-12">
					<div class="alert alert-warning" role="alert">
						 <button type="button" class="close" data-dismiss="alert">×</button>
						 <strong>警告!</strong> 请注意你的个人隐私安全.
					</div>
                        <?php
                            $form = ActiveForm::begin(
                                [
                                    'options' => ['class' => 'form-horizontal'],
                                    // 'action' => '/core/model/edit',
                                    'method' => 'POST',
                                    'fieldConfig' => [
                                        'labelOptions' => ['class' => 'col-md-1 control-label'],
                                        'template' => '{label}<div class="col-md-2 controls">{input}</div>{hint}{error}',
                                    ],
                                ]
                            );
                        ?>
                        <input type="hidden" name="id" value="<?php echo $model->id; ?>" />
                        <?= $form->field($model, 'modelName')->textInput(); ?>
                        <?= $form->field($model, 'desc')->textInput(); ?>
						<div class="form-group">
                            <label class="col-md-1 control-label" for="modelName">字段列表</label>
                            <div class="col-md-4 controls">
                                <ul class="list-group" id="fieldList">
                                <?php foreach ($model->getFieldSet() as $field) { ?>
                        		    <li class="list-group-item">
                                        <input type="hidden" name="fields[]" value="<?= $field['_id'] ?>">
                                        <span><?php echo $field->name; ?></span>
                                        <span><?php echo $field->desc; ?></span>
                                        <span class="glyphicon glyphicon-remove pull-right" onclick="fl.delField(this, '<?= $field->id ?>')"></span>
                                    </li>
                                <?php } ?>
                                </ul>
								<div class="pull-right">
                                    <button type="button" class="btn btn-default" style="" data-toggle="modal" data-target="#myModal">
                                        <span class="glyphicon glyphicon-plus"></span>
                                    </button>
                                </div>
							</div>
						</div>
                        <div class="form-group">
                        	<div class="col-md-12 controls">
                                <a type="button" class="btn btn-primary" href="/core/model/info?id=<?= $model->id ?>">取消</a>
                                <button type="submit" class="btn btn-primary">提交</button>
                        	</div>
                        </div>
                    <?php ActiveForm::end(); ?>
				</div>
			</div>
		</div>
	</div>
</div>


<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" id="myModal">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">添加字段</h4>
      </div>
      <div class="modal-body">
        <table class="table table-striped">
            <tr>
                <th>#</th>
                <th>字段名</th>
                <th>字段描述</th>
                <th>字段类型</th>
                <th> . . . . . .</th>
            </tr>
        </table>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
